package code.google.jcontainer.support.log;

import code.google.jcontainer.resolver.MethodAnnotationResolver;
import code.google.jcontainer.resolver.MethodResolveContext;
import code.google.jcontainer.resolver.ResolverException;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Method;

/**
 * &#64;{@link code.google.jcontainer.support.log.Log} resolver, just print a log info. 
 *
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 */
public class LogAnnotationResolver implements MethodAnnotationResolver{

    private static final org.apache.commons.logging.Log log = LogFactory.getLog(LogAnnotationResolver.class);

    public void resolveMethodAnnotationBeforeConstruct(MethodResolveContext methodResolveContext) throws ResolverException {

    }

    public void resolveMethodAnnotationAfterConstruct(MethodResolveContext methodContext) throws ResolverException {

    }

    public void resolveMethodAnnotationBeforeMethod(MethodResolveContext methodResolveContext) throws ResolverException {
        Object component = methodResolveContext.getComponent();
        Method method = methodResolveContext.getMethod();
        methodResolveContext.setUserData("start_time", System.currentTimeMillis());
        log.info("Enter Method: " + component.getClass().getName() + "." + method.getName());
    }

    public void resolveMethodAnnotationAfterMethod(MethodResolveContext methodResolveContext) throws ResolverException {
        Object component = methodResolveContext.getComponent();
        Method method = methodResolveContext.getMethod();
        Object result = methodResolveContext.getResult();
        long timeConsumed = System.currentTimeMillis() - (Long)methodResolveContext.getUserData("start_time");
        log.info("Exit Method: " + component.getClass().getName() + "." + method.getName() + ", consumed " + timeConsumed + "ms, result: " + result );
    }
}
